const { regeneratorRuntime, dayjs } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    async asyncAdsPostionSettingData(data) {
      return dispatch(global.activityReducer.asyncAdsPostionSettingData(data))
    },
    async asyncAdsBlockSettingSubmit(data) {
      return dispatch(global.activityReducer.asyncAdsBlockSettingSubmit(data))
    },
    async initPageImageSelect(data) {
      return await dispatch(global.globalReducer.initPageImageSelect(data))
    },
  })
)({
  data: {
    tplImageId: '',
    tplImageName: '',
    tplImageUrl: '',
    tplMaterialId: '',
    tplMaterialName: '',
    tplRemark: '',
    showTextarea: true
  },
  onLoad() {
    console.log(this.route)
    const {
      remark,
      materiel_id, materiel_name,
      activity_image_name, activity_image_id, activity_image_url
    } = this.data.mosStep2
    this.setData({
      tplImageId: activity_image_id || '',
      tplImageName: activity_image_name || '',
      tplImageUrl: activity_image_url || '',

      tplMaterialId: materiel_id || '',
      tplMaterialName: materiel_name || '',

      tplRemark: remark || '',
    })
  },
  onShow(e) {
    console.log('onShow', e)
    const { imageData } = this.data
    const checkedItem = imageData.find(i => i.checked)


    if (checkedItem) {
      this.asyncAdsPostionSettingData({
        ...this.data.mosStep1,
        image_id: checkedItem.id,
        image_name: checkedItem.image_name,
        image_url: checkedItem.image_url,
      })
      // this.setData({
      //   tplImageId: checkedItem.id,
      //   tplImageName: checkedItem.image_name,
      //   tplImageUrl: checkedItem.image_url,
      // })
    }
    this.initPageImageSelect({
      multiple: false,
      imageData: []
    })
  },
  handleLicon1Tap() {
    wx.navigateBack()
  },
  handleNextStep(e) {
    console.log('handleNextStep', e)
    const { image_id, materiel_id } = this.data.mosStep1
    if (!image_id || !materiel_id) {
      this.selectComponent('#toast').handleShow({
        message: '请补充广告位信息'
      })
      return
    }
    this.asyncAdsBlockSettingSubmit()
    wx.navigateBack()
  },
  handleTextChange(e) {
    // console.log('handleTextChange', e)
    // this.setData({ tplRemark: e.detail.value })
    this.asyncAdsPostionSettingData({
      ...this.data.mosStep1,
      remark: e.detail.value
    })
  },
  handleHideEvent(e){
    this.setData({ showTextarea: true })
  },
  handleShowMaterialDialg(e) {
    console.log('handleShowMaterialDialg', e)
    this.setData({ showTextarea: false })

    const { materialList, tplMaterialId } = this.data
    const copy = JSON.parse(JSON.stringify(materialList.data))
    copy.map(i => {
      i.checked = false
      i.show = true
    })
    const checkedItem = copy.find(i => i.id === tplMaterialId)
    console.log('checkedItem', checkedItem, copy)
    if (checkedItem) {
      checkedItem.checked = true
    }
    this.selectComponent('#material').handleShow(null, copy)
  },
  handleMaterial(e) {
    console.log('handleMaterial', e)
    const { cb, data } = e.detail
    const one = data.find(i => i.checked)
    this.asyncAdsPostionSettingData({
      ...this.data.mosStep1,
      materiel_id: one.id,
      materiel_name: one.name
    })
    // const tplMaterialId = one.id
    // const tplMaterialName = one.name
    // this.setData({ tplMaterialId, tplMaterialName })
    cb && cb()
  },
  handleEffectImage(e) {
    console.log('handleEffectImage', e)
    const copy = JSON.parse(JSON.stringify(this.data.activityImageList.data))
    copy.map(i => i.selected = false)
    this.initPageImageSelect({
      imageData: copy
    })
    global.handleRoute({ route: 'image-select' })
  }
}))